const tree = {
    name: 'root',
    children: [
        {
            name: 'c1',
            children: [
                {
                    name: 'c11',
                    children: []
                },
                {
                    name: 'c12',
                    children: []
                }
            ]
        },
        {
            name: 'c2',
            children: [
                {
                    name: 'c21',
                    children: []
                },
                {
                    name: 'c22',
                    children: []
                }
            ]
        }
    ]
}

let res = []
function getTreeAllName(tree) {
    const {name,children} = tree

    res.push(name)
    if (children) {
        children.forEach(item => getTreeAllName(item))
    }

    return res

}

console.log(getTreeAllName(tree));
